home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cppl1a.zip / ATCNV.C < prev    next >
C/C++ Source or Header  |  1990-07-15  |  6KB  |  218 lines

  1. /*
  2.     atcnv.c -- At-Sign Conversion Program
  3.  
  4.     This program converts the OPERATOR.ASC file to contain the embedded printer
  5. codes.  Embedded printer codes are for the HP-LaserJet II series of printers.
  6. it should be easy to change this to work with other types of printers, just
  7. change the spcode and epcode array and recompile the program.  Note the
  8. character in the string array is automatically changed to an "Escape"
  9. Character if it is the "@" character.  Written in TURBO C, by Steve Booth.
  10. EMail: EXEC-Pc.
  11.  
  12.      This code is freeware with no strings attached except that I don't want it 
  13. released commercially (it would be too embarassing).
  14. */
  15. #define NBRCDES 30
  16. #define NBRITM 5
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <dos.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. /* Subroutines: */
  24.  
  25. int   strchk (char *, char *);
  26. char *strcmatch(char *, char *);
  27. int strochg(char *, int, int);
  28.  
  29.  
  30. void main()
  31. {
  32. char  dli[250];          /* data line (input) */
  33. char  dlo[250];         /* data line (output) */
  34.  
  35. char  epcode[NBRITM][NBRCDES] = {
  36.               "@(s0S",      /* End Italic */
  37.               "@(s0B",      /* End Bold   */
  38.               "\0",         /* Indent 5   */
  39.               "\0",         /* Subtract 5 */
  40.               "@(s0B" };    /* Page Eject */
  41.  
  42. char  filin[40]  = "OPERATOR.ASC";  /* File Name: Input     */
  43. char  filout[40] = "TEXT.OUT";      /* File Name: Output    */
  44. FILE *fptri;                        /* file pointer: input  */
  45. FILE *fptro;                        /* file pointer: output */
  46. int   idx = 0;
  47. int   len;
  48.  
  49. char  lst[NBRITM][10]= {
  50.               "I(",
  51.               "b(",
  52.               "BEGIN(",
  53.               "END(",
  54.               "SECTION(" };
  55.  
  56. char *ptr;
  57. char *ptri;
  58. char *ptro;
  59. char *ptr2;
  60. int   ret;
  61.  
  62. char  spcode[NBRITM][NBRCDES] = {
  63.               "@(8U@(s1S",      /* Start Italic */
  64.               "@(8U@(s3B",      /* Start Bold */
  65.               "@(8U@&a6L",      /* Indent 5   */
  66.               "@(8U@&a1L",      /* Subtract 5 */
  67.               " @(8U@(s3B"};    /* Page Eject - Stuffed Later + Bold */
  68.  
  69.  
  70. spcode[4][0] = 12;          /* Place Page Eject into Array */
  71. while (idx  <= NBRITM) {    /* Insert Escape code into Array */
  72.   strochg(epcode[idx], '@', 27);
  73.   strochg(spcode[idx], '@', 27);
  74.   idx++;
  75.   }
  76. printf("atcnv - at-sign conversion program for HP-LJ printer.\n");
  77. printf("        written by S. Booth on 07/15/90\n\n");
  78.  
  79. printf("Opening file:  %s, for reading.\n",filin);
  80. if ( (fptri = fopen(filin,"r")) == NULL ) {
  81.      fprintf(stderr,"Error opening fileto read.");
  82.      _exit(1);
  83.      }
  84. printf("Creating file: %s.\n",filout);
  85. if ( (fptro = fopen(filout,"w")) == NULL ) {
  86.      fprintf(stderr,"Error opening file to write.");
  87.      fclose(fptri);
  88.      _exit(1);
  89.      }
  90.  
  91. while ( fgets(dli,250,fptri) != NULL ) /* Read in Data Line */
  92.   {
  93.   ptri = dli;
  94.   ptro = dlo;
  95.   dlo[0] = '\0';
  96.   while ( (ptr = strchr(ptri,'@')) != NULL ) {
  97.  
  98.   /*  Something to process. */
  99.  
  100.     ptr++;
  101.     idx = 0;
  102.  
  103.     while (idx <= NBRITM) {  /* Look for a Matching Name */
  104.  
  105.        ret = strchk(ptr, lst[idx]);
  106.        if (!ret) break;
  107.        idx++;
  108.        }  /* End While (idx) */
  109.  
  110. /* at this point, the list of all valid entries has been searched.
  111.       ret will tell if we've been successful. */
  112.  
  113.     if (!ret) { /* Match Found */
  114.        ptr -= 2;                   /* position to just before the match      */
  115.        len = ptr - ptri + 1;       /* number of characters to copy           */
  116.        memcpy(ptro, ptri, len);    /* first, copy string up to point of match */
  117.        ptro += len;                /* new position in "dlo"         */
  118.        ptri = ptr + strlen(lst[idx]) + 2;  /* new position in "dli" */
  119.        len  = strlen(spcode[idx]); /* add in starting printer codes */
  120.        memcpy(ptro, spcode[idx], len);  /* Copy Printer Codes */
  121.        ptro += len;
  122.        ptr2 = strcmatch(ptri, "()");     /* Find closing delimiter */
  123.        len  = ptr2 - ptri;               /* Number of characters between */
  124.        memcpy(ptro, ptri, len);          /* Copy input data */
  125.        ptro += len;
  126.        ptri = ptr2 + 1;
  127.  
  128.        if ( epcode[idx] ) {  /* More processing */
  129.           len = strlen(epcode[idx]);
  130.           memcpy(ptro,epcode[idx],len);
  131.           ptro += len;
  132.           }
  133.  
  134.        }  /* Match Found */
  135.   }  /* End While (ptr2) */
  136.  
  137.   len = strlen(ptri) + 1;
  138.   memcpy(ptro, ptri, len);
  139.   fputs(dlo, fptro);      /* Write Data Line */
  140.   }
  141. fputs(spcode[4],fptro);
  142. fclose(fptri);
  143. fclose(fptro);
  144.  
  145. }  /*  End of main */
  146.  
  147. int strchk(char *str, char *str2)
  148. /*
  149.      strchk checks "n" characters of str2 for matching.
  150.             returns 1 = no match.
  151.                     0 = match.   */
  152. {
  153. int idx = 0;
  154.  
  155. while (str2[idx]) {
  156.    if ( str[idx] != str2[idx] ) return(1);
  157.    idx++;
  158.    }
  159. return(0);
  160. }
  161.  
  162. char *strcmatch(char *str, char *str2)
  163. /*
  164.   strcmatch checks "n" characters of str2 for matching.
  165.             returns pointer to matching character; NULL if no match.
  166.  
  167.             str = string to be searched (assumed to be zero terminated).
  168.            str2 = matching pair.  E.g., "()".
  169. */
  170. {
  171. int idx = 0;
  172. int flgctr = 0;
  173. char *ptr;
  174.  
  175. while (str[idx]) {
  176.    if ( str[idx] != str2[1] ) {
  177.       if ( str[idx] == str2[0]) flgctr++;
  178.       }
  179.    else {
  180.       ptr = str + idx;
  181.       if ( !flgctr )
  182.          return(ptr);
  183.       else
  184.          flgctr--;
  185.       }
  186.    idx++;
  187.    }
  188. return(0);
  189. }
  190. int strochg(char *str1, int schr, int rchr)
  191. /*
  192.     This routine changes all occurrences of "schr" into "rchr" in
  193.          str1.
  194.  
  195.         str1 = string to be modified.
  196.         schr = integer character to be replaced.
  197.         rchr = integer replacement character.
  198.  
  199.     The number of changes made is returned.
  200. */
  201. {
  202. int idx = 0;
  203. int cnt = 0;
  204.  
  205. while (str1[idx]) {
  206.    if ( str1[idx] == schr ) {
  207.       cnt++;
  208.       str1[idx] = rchr;
  209.       }
  210.    idx++;
  211.    }
  212. return(cnt);
  213.  
  214. /*******************************************
  215.  *  end of strochg                         *
  216.  *******************************************/
  217.  
  218. }